﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using smartphone.App_Code;
using System.Data;
using System.Text;

namespace smartphone.UserControls
{
    public partial class AdminOrders : System.Web.UI.UserControl
    {
        private Order order = new Order();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void BtRecent_Click(object sender, EventArgs e)
        {
            int count;
            if (Int32.TryParse(TxtRecent.Text, out count))
            {
                GridViewOrders.DataSource = order.OrdersGetByRecent(count);

            }
            else
            {
                ShowPopUpMsg("Bạn cần nhập giá trị hợp lệ.");
            }
            GridViewOrders.DataBind();
        }

        protected void BtFromTo_Click(object sender, EventArgs e)
        {
            if ((Page.IsValid) && TxtStartDay.Text + TxtEndDay.Text != null)
            {
                DateTime start = DateTime.ParseExact(TxtStartDay.Text, "yyyy-MM-dd", null);
                DateTime end = DateTime.ParseExact(TxtEndDay.Text, "yyyy-MM-dd", null);

                GridViewOrders.DataSource = order.GetByDate(start, end);
            }
            else
            {
                errorLabel.Text = "Ngày nhập không hợp lệ.";
            }

            GridViewOrders.DataBind();
        }

        protected void BtUnverifiedUncancled_Click(object sender, EventArgs e)
        {
            GridViewOrders.DataSource = order.GetUnverifiedUncanceled();
            GridViewOrders.DataBind();
        }

        protected void BtVerifiedUncompleted_Click(object sender, EventArgs e)
        {
            GridViewOrders.DataSource = order.GetVerifiedUncompleted();
            GridViewOrders.DataBind();
        }

        private void ShowPopUpMsg(string msg)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("alert('");
            sb.Append(msg.Replace("\n", "\\n").Replace("\r", "").Replace("'", "\\'"));
            sb.Append("');");
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "showalert", sb.ToString(), true);
        }

        protected void GridViewOrders_SelectedIndexChanged(object sender, EventArgs e)
        {
            string destination = String.Format("AdminOrderDetailPage.aspx?OrderId={0}", GridViewOrders.DataKeys[GridViewOrders.SelectedIndex].Value.ToString());
            Response.Redirect(destination);
        }

    }
}